package concurrent.aqs;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * AQS源码解析
 *
 *
 * @author LoneRanger
 */
public class AQSReentrantLockTest {

	public static void main(String[] args) throws InterruptedException {
		ReentrantLock reentrantLock = new ReentrantLock();
		new Thread(() -> {
			reentrantLock.lock();
			try {
				System.out.println(Thread.currentThread().getName() + "进入窗口办理业务");
				try {
					TimeUnit.SECONDS.sleep(1);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			} catch (Exception e) {

			} finally {
				reentrantLock.unlock();
				System.out.println(Thread.currentThread().getName() + "业务办理结束");
			}
		}, "线程1").start();


		TimeUnit.SECONDS.sleep(2);
		new Thread(() -> {
			reentrantLock.lock();
			try {
				System.out.println(Thread.currentThread().getName() + "进入窗口办理业务");
			} catch (Exception e) {

			} finally {
				reentrantLock.unlock();
				System.out.println(Thread.currentThread().getName() + "业务办理结束");
			}
		}, "线程2").start();

		TimeUnit.SECONDS.sleep(2);
		new Thread(() -> {
			reentrantLock.lock();
			try {
				System.out.println(Thread.currentThread().getName() + "进入窗口办理业务");
			} catch (Exception e) {

			} finally {
				reentrantLock.unlock();
				System.out.println(Thread.currentThread().getName() + "业务办理结束");
			}
		}, "线程3").start();
	}

}
